/*
 * @Author: namelll 2677065112@qq.com
 * @Date: 2023-06-05 15:45:56
 * @LastEditors: namelll 2677065112@qq.com
 * @LastEditTime: 2023-06-14 15:26:27
 * @FilePath: \vue3-ts-project\src\store\modules\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: namelll 2677065112@qq.com
 * @Date: 2023-06-05 15:45:56
 * @LastEditors: namelll 2677065112@qq.com
 * @LastEditTime: 2023-06-05 16:24:56
 * @FilePath: \vue3-ts-project\src\store\modules\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//创建用户相关的小仓库
import { defineStore } from 'pinia';
// 引入登录请求接口
import { reqLogin, reqUserInfo, reqLogout } from '@/api/user';
//引入登录请求loginForm数据类型
import type { loginFormData, userInfoResponseData, loginResponseData } from "@/api/user/type";
//引入小仓库state数据类型
import type { UserState } from './types/type';
//引入操作本地存储的工具方法
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/token';
//引入路由(常量路由)
import { constantRoute, asnycRoute, anyRoute } from '@/router/routes';

//引入深拷贝方法
//@ts-ignore
// import cloneDeep from 'lodash/cloneDeep'
// import router from '@/router';

// //用于过滤当前用户需要展示的异步路由
// function filterAsyncRoute(asnycRoute: any, routes: any) {
//     return asnycRoute.filter((item: any) => {
//         if (routes.includes(item.name)) {
//             if (item.children && item.children.length > 0) {
//                 //硅谷333账号:product\trademark\attr\sku
//                 item.children = filterAsyncRoute(item.children, routes);
//             }
//             return true;
//         }
//     })
// }

//创建用户小仓库
let useUserStore = defineStore('User', {
    //小仓库存储数据地方
    state: (): UserState => {
        return {
            token: GET_TOKEN(),//用户唯一标识
            menuRoutes: constantRoute,//仓库存储生成菜单需要数组(路由)
            username: '',
            avatar: '',
            //存储当前用户是否包含某一个按钮
            // buttons:[],
        }
    },
    //异步|逻辑的地方
    actions: {
        //用户登录
        // 定义为异步函数，返回一个Promise对象，使用then方法或者await关键字来获取其返回值。
        async userLogin(data: loginFormData) {
            let result: loginResponseData = await reqLogin(data)
            //登录请求:成功200->获取token 
            //登录请求:失败201->错误
            if (result.code == 200) {
                // pinia仓库存储一下token
                // pinia|vuex存储数据其实利用js对象
                this.token = (result.data as string);
                //本地存储持久化存储一份
                SET_TOKEN((result.data as string));
            } else {
                return Promise.reject(new Error(result.data));
            }
        },
        //获取用户信息
        async userInfo() {
            //存储在仓库中
            let result: userInfoResponseData = await reqUserInfo()
            console.log("用户信息：", result)
            if (result.code == 200) {
                this.username = result.data.name
                this.avatar = result.data.avatar
                return 'ok'
            } else {
                return Promise.reject(new Error(result.message))
            }
        },
        //退出登录
        async userLogout() {
            let result:any = await reqLogout()
            if (result.code == 200) {

                this.token = '';
                this.username = '';
                this.avatar = '';
                REMOVE_TOKEN();
                return 'ok'
            } else {
                return Promise.reject(new Error(result.message))
            }

        },
        getters: {

        },
    }
})


//对外暴露获取小仓库方法
export default useUserStore;